In this demonstration
you will learn synchronous communication
between different services, create three services
that are customer, product, and order. In this demo, you will learn
about customer service. The structure has
been already created, so you have Customer, Product, and Order
microservices. Here, the dbconfig class has the dbconfig.js file
with all the codes related to connection
with MongoDB. Inside the Customer class, you have the router, controller, service, DAO, model,
and index.js file. The same files are available
inside the Product service. In the Order class, create the router, model, and index.js file. You can also have
the other.jsfile, however in this demo creating
the file is not required. The package.json file is also available in
outside the folders. Now, get started with the
configuration of the database. Inside the dbconfig, go to the dbfile. Use the module expressed to
import the express module. Import mongoose, which
has an inbuilt module as mongoose will help the program to
connect with mongoDb. Add a URL to give the
path for mongoDb. For a local system write localhost and then
the database name. Write the the database
name as orderDb. The database name can
be a random name. Now, create the app and call the connect method on mongoose to connect
with the database. Pass the URL and
add the URL parser, which is use new URL parser
with the value as true. Establish a connection using the mongoose.connection command. The command will return a
connection object that you will store in a variable,
say connection. On the connection variable
called the on method. The on method will
take two parameters. The first parameter
will be open event, and second parameter
is a callback. Write console.log to check if you are getting the
connection or not. Add a middleware that is
app.use(express.json()). You are done with
the connection file, now go to the customer class. First, create the model. In the module.jsfile
import mongoose. Also create a schema and store the schema in the
customerSchema variable. Use the mongoose.schema method. Add some attributes
to the command. Write the customer IS
which is of type:String. Add true since you require
the value of the customer ID, then add the customerName with the type String and
require value as true. Similarly, create the customerAge
and the customerAddress. Create a model with
the variable customer. On the mongoose object, add the model name and
the customerSchema. Export the module
which is the customer. You have completed
the model file, now go to the
customerDAO.jsfile. Add required dbconfig/dbfile. Create async function, save customer with
two parameters; customer object and
the done callback. Call await customer.
save method, which is the database method. Store the return value in
a variable called data. Return the callback with two
values; undefined and data. Similarly, create another
method called getCustomerById. The getCustomerById is also an async function with
three parameters: customer, customer ID, and the callback. Call the
customer.findById method and pass the customerId
as a parameter. Store the return value in
a variable called data. Once you get the data, return the callback with
undefined and data. Here you are done with
our DAO layer as well. Next, export the
saved customer and getCustomerById functionalities
from the DAO layer. Inside the service layer, import the customer DAO layer. Create the save customer
function which will have two parameters,
customer and done. Call the DAO layer, save customer method and
pass the parameters. Again, create the
getCustomerById method which will take three
parameters: customer, customerId, and done. Call customer DAO.getCustomerId
and pass the parameters. Export both the functionalities. Inside the controller, import customerService, create the function
saveCustomer, and call the
saveCustomer method. Pass the parameters. Second method is
getCustomerById. Pass all the three
parameters and call the service method
getCustomerId. Export both the
functionality. Thank you.